package com.mongdbtokuduswagger.controller;

import com.mongdbtokuduswagger.entity.SwaggerTimeEntity;
import com.mongdbtokuduswagger.service.MongodbSyncToKuduSwaggerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Daft_V
 */
@Controller
@Api(tags = "MongdbToKuduSwagger接口")
@RequestMapping("/mongdbToKuduSwagger")
public class MongdbToKuduSwaggerController {
    @Autowired
    MongodbSyncToKuduSwaggerService mongodbSyncToKuduSwaggerService;

    /**
     * 将mongodb的数据保存到Kudu,并删除mongodb中的数据
     * @param swaggerTimeEntity 时间范围实体类
     * @return 返回结果json
     * @throws ParseException
     */
    @PostMapping("/saveDataToKudu")
    @ApiOperation(value = "将mongodb的数据保存到Kudu,并删除mongodb中的数据")
    @ResponseBody
    public JSONObject saveDataToKudu(SwaggerTimeEntity swaggerTimeEntity) throws ParseException {
        Boolean flag = mongodbSyncToKuduSwaggerService.syncToKudu(swaggerTimeEntity);
        if (flag) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("massage", "Save Data To Kudu Success");
            JSONObject retJson = JSONObject.fromObject(map);
            return retJson;
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("code", 500);
            map.put("massage", "Save Data To Kudu Error");
            JSONObject retJson = JSONObject.fromObject(map);
            return retJson;
        }
    }

    /**
     * 删除MongoDb中今天以前的数据
     * @param swaggerTimeEntity 时间范围实体类
     * @return 返回结果json
     */
    @PostMapping("/deleteDataToMongodb")
    @ApiOperation(value = "删除MongoDb中今天以前的数据")
    @ResponseBody
    public JSONObject deleteDataToMongodb(SwaggerTimeEntity swaggerTimeEntity) {
        Boolean flag = mongodbSyncToKuduSwaggerService.deleteMongodb(swaggerTimeEntity);
        if (flag) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("massage", "Delete Mongodb Data Success");
            JSONObject retJson = JSONObject.fromObject(map);
            return retJson;
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("code", 500);
            map.put("massage", "Delete Mongodb Data Error");
            JSONObject retJson = JSONObject.fromObject(map);
            return retJson;
        }
    }

}
